﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using NXTMessaging;
using Microsoft.Xna.Framework;
using WindowsGame1.gamePlugins.manualsaves;
using WindowsGame1.gamePlugins.renderer;

namespace WindowsGame1.gamePlugins.nxt
{
    class ControllerBridgeNXT
    {
        public int calibrateP1Left = 0;

        public int calibrateP1Right = 360;

        public int calibrateP2Left = 0;

        public int calibrateP2Right = 360;

        public bool batRaaktBal = false;

        public int[] scores = new int[2];

        public int player1MailBox = 1;

        public int player2MailBox = 2;

        public int pointSendMailBox = 3;

        public int powerupSendMailBox = 4;

        public int resetSendMailBox = 5;

        public int multiplayerUpdateMailBox = 6;

        public int stopMoterMotionMailBox = 9;

        public int player2CPUMailBox = 10;

        // zorg dat je de waarden ongeveer van 1 tot 300 laat lopen of dergelijke
        public int betje1 = 0;

        public int betje2 = 0;

        //Communicatiepoort - standaard 11.
        private int com = 11;

        public int maxHeight = 720;

        private Receiving receiving;

        private Sending sending;

        public int ballx = 0;
        public int bally = 0;
        public int betje2YCoord = 0;
        public int maxdegreescount1 = 0;
        public int maxdegreescount2 = 0;
        public int currentDegrees1 = 0;
        public int currentDegrees2 = 0;

        public bool[] pu = new bool[7];
        //Lego NXT main communicator
        public NXTMessenger messenger;

        private ManualLoadPreference loadPref;

        public Game1 game;

        //Of de verbinding succesvol was.
        public bool successfullConnectAndAlive = false;

        public void collision(){
            batRaaktBal = true;
        }

        // nieuw
        public enum Calibrate_value
        {
            p1_left, p1_right, p2_left, p2_right
        }

        // nieuw
        public void calibrate(Calibrate_value welke)
        {
            // TODO:
            switch (welke)
            {
                case Calibrate_value.p1_left:
                    calibrateP1Left = currentDegrees1;
                    Console.WriteLine("p1 left calibrated: " + currentDegrees1);
                    break;
                case Calibrate_value.p1_right:
                    calibrateP1Right = currentDegrees1;
                    Console.WriteLine("p1 right calibrated: " + currentDegrees1);
                    break;
                case Calibrate_value.p2_left:
                    calibrateP2Left = currentDegrees2;
                    Console.WriteLine("p2 left calibrated: " + currentDegrees2);
                    break;
                case Calibrate_value.p2_right:
                    calibrateP2Right = currentDegrees2;
                    Console.WriteLine("p2 right calibrated: " + currentDegrees2);
                    break;
            }

        }

        /*
         * Maakt de verbinding, speelt een geluidje af als er een succesvolle verbinding heeft plaatsgevonden.
         * Zet mailbox 1 open.
         */
        public void init()
        {
            messenger = new NXTMessenger();
            //Search for a valid connection with Lego NXT robot.
            successfullConnectAndAlive = messenger.Connect("COM9");
            if (successfullConnectAndAlive)
            {
                //Maak geluidje ^^
                messenger.Beep();
                //Enable lees buffering op mailbox 1.
                messenger.EnableReadBuffering(1, true);
                messenger.EnableReadBuffering(2, true);
                messenger.EnableReadBuffering(3, true);
                messenger.EnableReadBuffering(4, true);
                messenger.EnableReadBuffering(5, true);
                messenger.EnableReadBuffering(6, true);
                sending = new Sending(this);
                receiving = new Receiving(this);
            }
        }

        /*
         * Maakt de verbinding ongedaan.
         */
        public void unload()
        {
            if (successfullConnectAndAlive)
            {
                receiving.getThread().Abort();
                receiving.getThread2().Abort();
                sending.getThread().Abort();
                //messenger.SendMessage("end", 4);
                messenger.Disconnect();
            }
        }

        /*
         * Maakt een instance aan, laad content en maakt connectie.
         */
        public ControllerBridgeNXT(Game1 game)
        {
            this.game = game;
            //Hier wordt de COM poort uit een bestandje gehaald
            loadPref = new ManualLoadPreference();
            com = Convert.ToInt32(loadPref.getFileContent("Content/COM.txt"));
            //Maak connectie met robot
            init();
        }

        /*
         * Wordt elke X (milli)seconden aangeroepen. 
         */
        public void update(GameTime gameTime, bool[] pu, Score score)
        {
            this.scores[0] = score.player1score;
            this.scores[1] = score.player2score;
            this.pu = pu;
        }

        public void scoreChanged(int i, int j)
        {
        }

        public float multiplayerCPU(int x, int y, int betje2Y)
        {
            betje2YCoord = betje2Y;
            ballx = x;
            bally = y;
            return 0;
        }

        private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        public static long getCurrentTimeMillis()
        {
            return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
        }
    }    
}